package com.huike.web.controller.system;

import com.huike.clues.MpService.Impl.MpSysRoleServiceImpl;
import com.huike.clues.MpService.MpSysRoleService;
import com.huike.clues.domain.dto.MPSysRoleDto;
import com.huike.clues.domain.result.PageResult;
import com.huike.clues.service.ISysRoleService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysRole;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.entity.MPSysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.huike.common.core.controller.BaseController;
import javax.annotation.Resource;

import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;


/**
 * 角色信息相关接口
 */
@RestController
@RequestMapping("/system/role")
@Slf4j
@Api(tags = "角色信息相关接口")
public class SysRoleController extends BaseController{

    @Resource
    private MpSysRoleService mpSysRoleService;

    /**
     * 分页查询角色列表接口
     * @param mpSysRole
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @ApiOperation("分页查询角色列表接口")
    @GetMapping("/list")
    public TableDataInfo roleList(MPSysRole mpSysRole){
       log.info("分页查询开始");
        PageResult pageResult = mpSysRoleService.roleList(mpSysRole);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(pageResult.getTotal());
        tableDataInfo.setRows(pageResult.getRecords());
        return tableDataInfo;
    }


    /**
     * 新增角色接口
     * @param mpSysRoleDto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @ApiOperation("新增角色接口")
    @PostMapping
    public AjaxResult addRole(@RequestBody MPSysRoleDto mpSysRoleDto){
        log.info("新增角色：{}",mpSysRoleDto.getRoleName());
        mpSysRoleService.addRole(mpSysRoleDto);
        return AjaxResult.success();
    }

    /**
     * 根据角色编号获取详细信息
     * @param roleId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @ApiOperation("根据角色编号获取详细信息")
    @GetMapping("/{roleId}")
    public AjaxResult selectById(@PathVariable Integer roleId){
        log.info("根据角色编号获取详细信息");
        MPSysRoleDto mpSysRoleDto = mpSysRoleService.selectById(roleId);
        return AjaxResult.success(mpSysRoleDto);
    }


    /**
     * 修改保存角色
     * @param mpSysRoleDto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @ApiOperation("修改保存角色")
    @PutMapping
    public AjaxResult updateRole(@RequestBody MPSysRoleDto mpSysRoleDto){
        log.info("修改保存角色");
        mpSysRoleService.updateRole(mpSysRoleDto);
        return AjaxResult.success();
    }

    /**
     * 状态修改
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @PutMapping("/changeStatus")
    @ApiOperation("状态查询")
    public AjaxResult updateByStatus(@RequestBody MPSysRoleDto mpSysRoleDto){
        log.info("状态修改开始");
        mpSysRoleService.updateByStatus(mpSysRoleDto);
        return AjaxResult.success();
    }


    /**
     * export
     * @param mpSysRoleDto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:export')")
    @ApiOperation("export")
    @GetMapping("/export")
    public AjaxResult export(MPSysRoleDto mpSysRoleDto){
        log.info("export开始了");
        mpSysRoleService.export(mpSysRoleDto);
        return AjaxResult.success();
    }

    /**
     * 获取角色选择框列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @ApiOperation("获取角色选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult optionSelect(){
        log.info("获取角色选择框列表");
        List<MPSysRoleDto> sysRoleDtoList = mpSysRoleService.optionselect();
        return AjaxResult.success(sysRoleDtoList);
    }


    /**
     * 修改保存数据权限
     * @param mpSysRoleDto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @ApiOperation("修改保存数据权限")
    @PutMapping("/dataScope")
    public AjaxResult dataScope(@RequestBody MPSysRoleDto mpSysRoleDto){
        mpSysRoleService.dataScope(mpSysRoleDto);
        return AjaxResult.success();
    }


    /**
     * 批量删除角色数据
     * @param roldIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @ApiOperation("")
    @DeleteMapping("/{roldIds}")
    public AjaxResult deleteById(@PathVariable List<Long> roldIds){
        mpSysRoleService.deleteById(roldIds);
        return AjaxResult.success();
    }
}
